<?php
/**
 * 
 * 2013-10-21 UTF-8
 * @author Rex Lee
 *
 */
class AdminAction extends Action {
	public $videoModel=NULL;
	function _initialize() {
		session_start();//启动session
		$this->videoModel=M('video', 'vv_', 'DB_CONFIG1');
	}
	
	function index() {
		
		if(!session('admin')){
			header('Location: '.U('admin/login'));
		}
		
		$data['title']=$_POST['title'];
		$data['url']=$_POST['url'];
		$data['description']=$_POST['desc'];
		$data['author']=$_POST['author'];
		$data['shot']=$_POST['cover'];
		
		if (($data['title']!=null)&&($data['url']!=null)&&($data['author']!=null)) {
			$rst=$this->videoModel->add($data);
		}
		
		if ($rst) {
			$this->show('添加成功！');
		}
		$this->display();
		
		
	}
	function remove() {
		
		if(!session('admin')){
			header('Location: '.U('admin/login'));
		}
		
		if ($_POST['vid']!=null) {
			$vid=(int)$_POST['vid'];
			$rst=$this->videoModel->where(array('id'=>$vid))->delete();
			M('vote', 'vv_', 'DB_CONFIG1')->where(array('vid'=>$vid))->delete();
			if ($rst) {
				$this->ajaxReturn(array('msg'=>'成功删除视频'.$vid));
			}else {
				$this->ajaxReturn(array('msg'=>'删除失败！'));
			}
		}
		$rst=$this->videoModel->select();
		$this->assign('list', $rst);
		$this->display();
	}
	function edit() {
		
		if(!session('admin')){
			header('Location: '.U('admin/login'));
		}
		
		$this->display();
	}
	function about() {
		
		if(!session('admin')){
			header('Location: '.U('admin/login'));
		}
		
		$this->display();
	}
	function login() {
		if (($_POST['username']!=null)&&($_POST['password']!=null)) {
			$username=$_POST['username'];
			$password=$_POST['password'];
			$rst=M('admin', 'vv_', 'DB_CONFIG1')->where(array('username'=>$username, 'password'=>$password))->find();
			if ($rst) {
				session('admin',$username);
				header('Location: '.U('admin/index'));
			}else {
				$this->show('登录失败！');
			}
		}
		
		$this->display();
	}
	
	function logout() {
		session_destroy();
		header('Location: '.U('admin/login'));
	}
}